package com.unnisworld.chess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: unni
 * Date: Mar 7, 2009
 * Time: 5:35:08 PM
 * To change this template use File | Settings | File Templates.
 */
class ChessBoard extends Frame
    {
        public static Map<String, Image> imageMap;

        static
        {
           imageMap = new HashMap<String, Image>(12);

           String userDir = System.getProperty("user.dir");

           BufferedImage img = null;
           try {
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/b_king.PNG"));
               imageMap.put("b_KING", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/w_king.PNG"));
               imageMap.put("w_KING", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/b_queen.PNG"));
               imageMap.put("b_QUEEN", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/w_queen.PNG"));
               imageMap.put("w_QUEEN", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/b_rook.PNG"));
               imageMap.put("b_ROOK", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/w_rook.PNG"));
               imageMap.put("w_ROOK", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/b_bishop.PNG"));
               imageMap.put("b_BISHOP", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/w_bishop.PNG"));
               imageMap.put("w_BISHOP", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/b_knight.PNG"));
               imageMap.put("b_KNIGHT", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/w_knight.PNG"));
               imageMap.put("w_KNIGHT", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/b_pawn.PNG"));
               imageMap.put("b_PAWN", img);
               img = javax.imageio.ImageIO.read(new File(userDir + "/img/w_pawn.PNG"));
               imageMap.put("w_PAWN", img);


           } catch (IOException e) {
               System.err.println("Exception while loading coins :"+e);
           }
        }

        static int mx=0,my=0;
        static boolean click = false;
        String msg=" ";
        static int srow1=0,scol1=0,srow2=0,scol2=0,p1=0,p2=0,selected=0;
        static Square b[][] = new Square[8][8];
        static int drawn = 0;
        static int nextMove = 1;
        static int buttonPressed = 0;

        MenuBar mb;
        Menu file,help;
        MenuItem open,save;
        //Frame f;

     ChessBoard()
        {

         mb = new MenuBar();
         file = new Menu("File");
         open = new MenuItem("Open");
         save = new MenuItem("Save");
         mb.add(file);
         file.add(open);
         file.add(save);
         this.setMenuBar(mb);
         MyMenuHandler h = new MyMenuHandler(this);
         open.addActionListener(h);
         save.addActionListener(h);

         this.setTitle("ChessBoard Game");
         this.setVisible(true);
         this.setSize(800,800);


         addMouseListener(new MyMouseAdapter(this));
         addWindowListener(new MyWindowAdapter());
         //drawn = 1;
        }
     public void paint(Graphics g)
        {
         if(drawn == 1)
         {
           for(int i = 0;i<8;i++)
            {
           for(int j = 0;j<8;j++)
             {
              b[i][j].paint(g);
             }
           }
         }// end of if drawn
        }

     private static ChessBoard c = null;

     public static void main(String args[])
        {
         int flag=1,color,left=10,top=50,right=60,bottom=50;
         Coin coin = Coin.EMPTY;   
         c = new ChessBoard();
         for(int i=0;i<8;i++) // filling objects
            {
            for(int j=0;j<8;j++)
               {
               coin = Coin.EMPTY;
               color=5;
               if(i==0 || i==1)
                 color=0;
               if(i==6 || i==7)
                 color=15;
               if( (i==0 && j==0) || (i==0 && j==7) || (i==7 && j==0) || (i==7 && j==7))
                 coin = Coin.ROOK;
               if( (i==0 && j==1) || (i==0 && j==6) || (i==7 && j==1) || (i==7 && j==6) )
                 coin = Coin.KNIGHT;
               if( (i==0 && j==2) || (i==0 && j==5) || (i==7 && j==2) || (i==7 && j==5) )
                 coin = Coin.BISHOP;
               if(i==1 || i==6)
                 {
                 coin = Coin.PAWN;
                 }
               if( (i == 0 && j==3) || (i==7 && j==3)  )
                 {
                 coin = Coin.QUEEN;
                 }
               if( (i == 0 && j ==4) || (i==7 && j==4) )
                 {
                  coin = Coin.KING;
                 }
                b[i][j] = new Square(left,top,50,50,coin,color,flag);
                b[i][j].paint(c.getGraphics());
                flag=1-flag;
                left+=50;
                right+=50;
                }
             top+=50;
             bottom+=50;
             left=10;
             right=60;
             flag=1-flag;
             }//end of for loop
             drawn = 1;
           c.repaint();
        }

      // This method is called from mouse pressed event handler.  
      public static void performAction() {
               if(click)
                {
                 int position = getPosition();
                    if(position > -1)
                      {
                       int row = position / 10;
                       int col = position - row * 10;
                       System.out.print(row + " " + col);
                         if(selected == 0) //if this is the first selection
                          {
                            if( b[row][col].getColor() != nextMove * 15)
                                {
                                 click = false;
                                 return;
                                }
                            if(buttonPressed == 4) // right click
                               {
                                if(b[row][col].getCoin() == Coin.ROOK && b[row][col].getStatus() == 0) //rook
                                   {

                                     System.out.println("Castling is possible");
                                     if( castle(row,col,c) == 1)
                                       {
                                        click = false;
                                        nextMove = 1 - nextMove;
                                       }
                                   }
                                buttonPressed = 0;
                                return;
                               }  // end of right click

                            if( b[row][col].getCoin() != Coin.EMPTY) //if the cell is not empty
                             {
                              p1  = position;
                              b[row][col].selected(c.getGraphics());
                              srow1 = row; scol1 = col;
                              selected = 1;
                             }
                            else
                              System.out.println("Cell is empty");
                          }
                         else //second selection
                          {
                           p2 = position;
                           srow2 = row; scol2 = col;
                           if(legalMove() == 1)
                               {
                                //c.repaint();
                                b[srow1][scol1].paint(c.getGraphics());
                                b[srow2][scol2].paint(c.getGraphics());
                                b[srow2][scol2].setStatus();
                                nextMove = 1 - nextMove;
                               }
                           selected = 0;
                          }
                      }
                 click = false;
                }
      }

      static int getPosition()
          {
           //System.out.print("unni");
           if( (mx >=10 && my >= 50) && (mx <=60 && my <=100) )
               return 0;
           if( (mx >=60 && my >= 50) && (mx <=110 && my <=100) )
               return 1;
           if( (mx >=110 && my >= 50) && (mx <=160 && my <=100) )
               return 2;
           if( (mx >=160 && my >= 50) && (mx <=210 && my <=100) )
               return 3;
           if( (mx >=210 && my >= 50) && (mx <=260 && my <=100) )
               return 4;
           if( (mx >=260 && my >= 50) && (mx <=310 && my <=100) )
               return 5;
           if( (mx >=310 && my >= 50) && (mx <=360 && my <=100) )
               return 6;
           if( (mx >=360 && my >= 50) && (mx <=410 && my <=100) )
               return 7;

           if( (mx >=10 && my >=100) && (mx <= 60 && my <= 150) )
                return 10;
           if( (mx >60 && my >100) && (mx <= 110 && my <= 150) )
                return 11;
           if( (mx >110 && my >100) && (mx <= 160 && my <= 150) )
                return 12;
           if( (mx >160 && my >100) && (mx <= 210 && my <= 150) )
                return 13;
           if( (mx >210 && my >100) && (mx <= 260 && my <= 150) )
                return 14;
           if( (mx >260 && my >100) && (mx <= 310 && my <= 150) )
                return 15;
           if( (mx >310 && my >100) && (mx <= 360 && my <= 150) )
                return 16;
           if( (mx >360 && my >100) && (mx <= 410 && my <= 150) )
                return 17;

          if( (mx >=10 && my >=150) && (mx <= 60 && my <= 200) )
                return 20;
          if( (mx >60 && my >150) && (mx <= 110 && my <= 200) )
                return 21;
          if( (mx >110 && my >150) && (mx <= 160 && my <= 200) )
                return 22;
          if( (mx >160 && my >150) && (mx <= 210 && my <= 200) )
                return 23;
          if( (mx >210 && my >150) && (mx <= 260 && my <= 200) )
                return 24;
          if( (mx >260 && my >150) && (mx <= 310 && my <= 200) )
                return 25;
          if( (mx >310 && my >150) && (mx <= 360 && my <= 200) )
                return 26;
          if( (mx >360 && my >150) && (mx <= 410 && my <= 200) )
                 return 27;

          if( (mx >=10 && my >=200) && (mx <= 60 && my <= 250) )
                 return 30;
          if( (mx >60 && my >200) && (mx <= 110 && my <= 250) )
                 return 31;
          if( (mx >110 && my >200) && (mx <= 160 && my <= 250) )
               return 32;
          if( (mx >160 && my >200) && (mx <= 210 && my <= 250) )
               return 33;
          if( (mx >210 && my >200) && (mx <= 260 && my <= 250) )
               return 34;
          if( (mx >260 && my >200) && (mx <= 310 && my <= 250) )
               return 35;
          if( (mx >310 && my >200) && (mx <= 360 && my <= 250) )
                return 36;
          if( (mx >360 && my >200) && (mx <= 410 && my <= 250) )
               return 37;

          if( (mx >10 && my >250) && (mx <= 60 && my <= 300) )
               return 40;
          if( (mx >60 && my >250) && (mx <= 110 && my <= 300) )
               return 41;
          if( (mx >110 && my >250) && (mx <= 160 && my <= 300) )
               return 42;
          if( (mx >160 && my >250) && (mx <= 210 && my <= 300) )
               return 43;
          if( (mx >210 && my >250) && (mx <= 260 && my <= 300) )
               return 44;
          if( (mx >260 && my >250) && (mx <= 310 && my <= 300) )
               return 45;
          if( (mx >310 && my >250) && (mx <= 360 && my <= 300) )
               return 46;
          if( (mx >360 && my >250) && (mx <= 410 && my <= 300) )
               return 47;

          if( (mx >10 && my >300) && (mx <= 60 && my <= 350) )
               return 50;
          if( (mx >60 && my >300) && (mx <= 110 && my <= 350) )
               return 51;
          if( (mx >110 && my >300) && (mx <= 160 && my <= 350) )
               return 52;
          if( (mx >160 && my >300) && (mx <= 210 && my <= 350) )
               return 53;
          if( (mx >210 && my >300) && (mx <= 260 && my <= 350) )
               return 54;
          if( (mx >260 && my >300) && (mx <= 310 && my <= 350) )
               return 55;
          if( (mx >310 && my >300) && (mx <= 360 && my <= 350) )
               return 56;
          if( (mx >360 && my >300) && (mx <= 410 && my <= 350) )
               return 57;

          if( (mx >10 && my >350) && (mx <= 60 && my <= 400) )
               return 60;
          if( (mx >60 && my >350) && (mx <= 110 && my <= 400) )
               return 61;
          if( (mx >110 && my >350) && (mx <= 160 && my <= 400) )
               return 62;
          if( (mx >160 && my >350) && (mx <= 210 && my <= 400) )
               return 63;
          if( (mx >210 && my >350) && (mx <= 260 && my <= 400) )
               return 64;
          if( (mx >260 && my >350) && (mx <= 310 && my <= 400) )
               return 65;
          if( (mx >310 && my >350) && (mx <= 360 && my <= 400) )
               return 66;
          if( (mx >360 && my >350) && (mx <= 410 && my <= 400) )
               return 67;

           if( (mx >10 && my >400) && (mx <= 60 && my <= 450) )
               return 70;
           if( (mx >60 && my >400) && (mx <= 110 && my <= 450) )
               return 71;
           if( (mx >110 && my >400) && (mx <= 160 && my <= 450) )
               return 72;
           if( (mx >160 && my >400) && (mx <= 210 && my <= 450) )
               return 73;
           if( (mx >210 && my >400) && (mx <= 260 && my <= 450) )
               return 74;
           if( (mx >260 && my >400) && (mx <= 310 && my <= 450) )
               return 75;
           if( (mx >310 && my >400) && (mx <= 360 && my <= 450) )
               return 76;
           if( (mx >360 && my >400) && (mx <= 410 && my <= 450) )
               return 77;

            return -1;
          }

        static int castle(int row,int col, ChessBoard c)
          {
            if(row == 0) // black rook
             {
               if(col == 0) // rook1
                {
                 if( (b[0][1].getCoin() == Coin.EMPTY)  && (b[0][2].getCoin() == Coin.EMPTY)
                         && (b[0][3].getCoin() == Coin.EMPTY))
                     {
                       b[0][0].setCoin(Coin.EMPTY, 5);
                       b[0][0].paint(c.getGraphics());
                       b[0][4].setCoin(Coin.EMPTY, 5);
                       b[0][4].paint(c.getGraphics());
                       b[0][3].setCoin(Coin.ROOK, 0);
                       b[0][3].paint(c.getGraphics());
                       b[0][2].setCoin(Coin.KING, 0);
                       b[0][2].paint(c.getGraphics());
                       return 1;
                     }
                }//end of rook1
              if(col == 7) // rook2
                {
                 if( (b[0][6].getCoin() == Coin.EMPTY) && (b[0][5].getCoin() == Coin.EMPTY) )
                   {
                    b[0][7].setCoin(Coin.EMPTY, 5);
                    b[0][7].paint(c.getGraphics());
                    b[0][4].setCoin(Coin.EMPTY, 5);
                    b[0][4].paint(c.getGraphics());
                    b[0][5].setCoin(Coin.ROOK, 0);
                    b[0][5].paint(c.getGraphics());
                    b[0][6].setCoin(Coin.KING, 0);
                    b[0][6].paint(c.getGraphics());
                    return 1;
                   }
                }//end of rook2

             }//end of black rook

            else if(row == 7) // white  rook
              {
               if(col == 0) //rook1
                 {
                 if( (b[7][1].getCoin() == Coin.EMPTY)  && (b[7][2].getCoin() == Coin.EMPTY)
                         && (b[7][3].getCoin() == Coin.EMPTY))
                     {
                       b[7][0].setCoin(Coin.EMPTY, 5);
                       b[7][0].paint(c.getGraphics());
                       b[7][4].setCoin(Coin.EMPTY, 5);
                       b[7][4].paint(c.getGraphics());
                       b[7][3].setCoin(Coin.ROOK, 15);
                       b[7][3].paint(c.getGraphics());
                       b[7][2].setCoin(Coin.KING,15);
                       b[7][2].paint(c.getGraphics());
                       return 1;
                     }
                 } //end of rook1
             if(col == 7)//rook2
               {
                 if( (b[7][6].getCoin() == Coin.EMPTY) && (b[7][5].getCoin() == Coin.EMPTY) )
                   {
                    b[7][7].setCoin(Coin.EMPTY, 5);
                    b[7][7].paint(c.getGraphics());
                    b[7][4].setCoin(Coin.EMPTY, 5);
                    b[7][4].paint(c.getGraphics());
                    b[7][5].setCoin(Coin.ROOK, 15);
                    b[7][5].paint(c.getGraphics());
                    b[7][6].setCoin(Coin.KING, 15);
                    b[7][6].paint(c.getGraphics());
                    return 1;
                   }
               } //end of rook2

            } //end of white rook
            return 0;
          }



        public static int legalMove()
          {
           Coin tcoin1 = b[srow1][scol1].getCoin();
           int tcolor1 = b[srow1][scol1].getColor();
           Coin tcoin2 = b[srow2][scol2].getCoin();
           int tcolor2 = b[srow2][scol2].getColor();
             switch(tcoin1)
                 {
                  //pawn
                  case PAWN:
                         if(pawnMove(tcoin1,tcolor1,tcoin2,tcolor2) == 1)//legal move
                            {
                             b[srow2][scol2].setCoin(Coin.PAWN, tcolor1);
                             b[srow1][scol1].setCoin(Coin.EMPTY, 5);
                             return 1;
                            }
                          break;
                  //rook
                  case ROOK:
                          if(rookMove(tcoin1,tcolor1,tcoin2,tcolor2) == 1)
                             {
                              b[srow2][scol2].setCoin(Coin.ROOK, tcolor1);
                              b[srow1][scol1].setCoin(Coin.EMPTY, 5);
                              return 1;
                             }
                           break;
                  //knight
                  case KNIGHT:
                         int tval = Math.abs(p2 - p1);
                         if( (tval == 8) || (tval == 12) || (tval == 19) || (tval == 21) )
                                {
                                 if( tcoin2 == Coin.EMPTY || (tcolor1 != tcolor2))
                                     {
                                      b[srow1][scol1].setCoin(Coin.EMPTY, 5);
                                      b[srow2][scol2].setCoin(Coin.KNIGHT, tcolor1);
                                      return 1;
                                     }
                                }//end of knight
                          break;
                  //bishop
                  case BISHOP:
                         if(bishopMove(tcoin1,tcolor1,tcoin2,tcolor2) == 1)
                             {
                              b[srow2][scol2].setCoin(Coin.BISHOP, tcolor1);
                              b[srow1][scol1].setCoin(Coin.EMPTY, 5);
                              return 1;
                             }
                          break;
                   //queen
                   case QUEEN:
                           if( (bishopMove(tcoin1,tcolor1,tcoin2,tcolor2) == 1) || ( rookMove(tcoin1,tcolor1,tcoin2,tcolor2) == 1 ) || (pawnMove(tcoin1,tcolor1,tcoin2,tcolor2) == 1) )
                                {
                                 b[srow2][scol2].setCoin(Coin.QUEEN, tcolor1);
                                 b[srow1][scol1].setCoin(Coin.EMPTY, 5);
                                 return 1;
                                }
                           break;
                   //king
                   case KING:
                          int tempking=Math.abs(p1-p2);
                          if(tcolor1 == tcolor2)
                                return 0;
                          if( (tempking == 1) || (tempking == 10) || (tempking == 9) || (tempking == 11) )
                              {
                                 b[srow2][scol2].setCoin(Coin.KING, tcolor1);
                                 b[srow1][scol1].setCoin(Coin.EMPTY, 5);
                                 return 1;
                              }
                 }//end of switch
            return 0;
          }

      public static int pawnMove(Coin tcoin1,int tcolor1,Coin tcoin2,int tcolor2)
         {
          if( tcolor1 == tcolor2) // both the cell have same color
             return 0;
          if(tcolor1 == 15) // to prevent backward movement
             {
              if(p2 > p1)
                 return 0;
             }//end of tcolor1 == 15
          if(tcolor1 == 0)
             {
               if(p2 < p1)
                  return 0;
             }//end of tcolor1 == 0

          int tval = Math.abs(p2 - p1);
          if(tcoin2 == Coin.EMPTY) //if the second cell is empty
              {
                if( (tval == 10) || (tval == 20) ) //straight move
                  {
                   if( tval == 20 ) //two cell move
                      {
                       if( (srow1 == 1) || (srow1 == 6) ) //first move
                            return 1;
                       else
                           {
                            System.out.println("i am here");
                            return 0;
                           }
                      }
                    else if( tval == 10 )// one cell move
                       {
                           return 1;
                       }
                  }
                return 0;
              } //end of second cell empty

           else // second cell is not empty
             {
              System.out.println("debugging");
              if( (tval == 11) || (tval == 9) )
                  {
                   System.out.println("at two");
                   return 1;
                  }
             }

         /* if( (srow1 == 1)  || (srow1 == 6) ) //checking for first move
            {

            }//end of first move*/
          return 0;
         }//end of pawn move

       public static int rookMove(Coin tcoin1,int tcolor1,Coin tcoin2,int tcolor2)
         {
          if(tcolor1 == tcolor2)// both are same color coins
                return 0;
          if(srow1 == srow2) // column wise movement
            {
             if(p2 > p1)
              {
               for(int i = scol1 + 1; i < scol2; i++)
                {
                 if(b[srow1][i].getCoin() != Coin.EMPTY)
                   return 0;
                } //end of for loop
               } //end of p2 > p1
             else if( p1 > p2)
               {
               for(int i = scol2 + 1; i < scol1; i++)
                {
                 if(b[srow1][i].getCoin() != Coin.EMPTY)
                   return 0;
                } //end of for loop
               } //end of p1 > p2
             return 1;
            }//end of column wise movement

          else if(scol1 == scol2) //row wise movement
            {
             if(p2 > p1)
               {
                for(int i = srow1 + 1; i< srow2;i+=1)
                  {
                  if(b[i][scol1].getCoin() != Coin.EMPTY)
                     return 0;
                  }//end of for loop
               }// end of p2 > p1
             else if(p1 > p2)
               {
                for(int i = srow2 + 1; i< srow1;i+=1)
                  {
                  if(b[i][scol1].getCoin() != Coin.EMPTY)
                     return 0;
                  }//end of for loop
               } //end of p1 > p2
             return 1;
            }//end of row wise movement

          return 0;
         }//end of rook move

     public static int bishopMove(Coin tcoin1,int tcolor1,Coin tcoin2,int tcolor2)
           {
            if(tcolor1 == tcolor2)
               return 0;
            int ktemp = Math.abs(p1-p2);
            if( (ktemp % 9) != 0) // if move is not legal
              {
               if( (ktemp % 11) != 0)
                   return 0;
              }//end of not legal move

            if( (ktemp % 9) == 0)
              {
               if(p1 < p2)
                 {
                  for(int i = p1+9; i<p2;i+=9)
                   {
                    int t = i / 10;
                    int t1 = i - t * 10;
                    if( b[t][t1].getCoin() != Coin.EMPTY) // if coin in between
                        return 0;
                   }//end of for loop
                 }//end of p1 < p2

               else if( p1 > p2)
                {
                  for(int i = p2+9; i<p1;i+=9)
                   {
                    int t = i / 10;
                    int t1 = i - t * 10;
                    if( b[t][t1].getCoin() != Coin.EMPTY) // if coin in between
                        return 0;
                   }//end of for loop
                } // end of p1 > p2
               return 1;
              } //end of % 9 == 0

             if( (ktemp % 11) == 0)
                {
                if(p1 < p2)
                  {
                   for(int i = p1+11; i<p2;i+=11)
                    {
                     int t = i / 10;
                     int t1 = i - t * 10;
                     if( b[t][t1].getCoin() != Coin.EMPTY) // if coin in between
                        return 0;
                   }//end of for loop
                 }//end of p1 < p2

               else if( p1 > p2)
                {
                  for(int i = p2+11; i<p1;i+=11)
                   {
                    int t = i / 10;
                    int t1 = i - t * 10;
                    if( b[t][t1].getCoin() != Coin.EMPTY) // if coin in between
                        return 0;
                   }//end of for loop
                } // end of p1 > p2
               return 1;
              }//end of ktemp % 11

               return 0;
           }//end of bishopMove

  /*********************/
   class MyMenuHandler implements ActionListener, ItemListener
     {
       ChessBoard c1;
       public MyMenuHandler(ChessBoard c)
         {
           c1 = c;
         }
       public void actionPerformed(ActionEvent ae)
          {
           System.out.println("In action performed");
           String arg = (String)ae.getActionCommand();
           if(arg.equals("Save"))
             {
              FileDialog fd = new FileDialog(c1,"Save",FileDialog.SAVE);
              fd.setVisible(true);
              System.out.println(fd.getDirectory());
              System.out.println(fd.getFile());
              String f = fd.getDirectory() + fd.getFile();
              try
                {
                 FileOutputStream out = new FileOutputStream(f);
                 DataOutputStream dout = new DataOutputStream(out);
                 ObjectOutputStream sout = new ObjectOutputStream(dout);


                 for(int i = 0;i<8;i++)
                   {
                    for(int j = 0;j<8;j++)
                     {
                       sout.writeObject(b[i][j]);
                     }
                   }//end of i
                  dout.writeInt(nextMove);
                  sout.close();
                }catch(IOException ex)
                 {
                 }//end of try-catch
             }//end of save
           else if(arg.equals("Open"))
             {
              FileDialog fd = new FileDialog(c1,"Save",FileDialog.LOAD);
              fd.setVisible(true);
              System.out.println(fd.getDirectory());
              System.out.println(fd.getFile());
              String f = fd.getDirectory() + fd.getFile();

              try
                {
                 FileInputStream in = new FileInputStream(f);
                 DataInputStream din = new DataInputStream(in);
                 ObjectInputStream sin = new ObjectInputStream(din);
                 for(int i=0;i<8;i++)
                   {
                    for(int j = 0;j<8;j++)
                     {
                      b[i][j]=(Square) sin.readObject();
                     }
                   }//end of for
                 nextMove=din.readInt();
                 sin.close();
                 c1.repaint();
                }catch(Exception ex)
                  {
                  }
             }
          }
       public void itemStateChanged(ItemEvent ie)
          {
          }
     }



  class MyMouseAdapter extends MouseAdapter
        {
         ChessBoard c;
         public MyMouseAdapter(ChessBoard c)
               {
                this.c = c;
               }
         public void mousePressed(MouseEvent me)
                {
                 buttonPressed = me.getModifiers();
                 System.out.println("The mouse button pressed: is " + buttonPressed);

                 c.mx=me.getX();
                 c.my=me.getY();
                 c.click = true;
                 c.msg = "Mouse at "+c.mx+" "+c.my;
                 performAction();   
                 //c.repaint();
                }
        }
      }  //end of ChessBoard class

class MyWindowAdapter extends WindowAdapter
        {
         public void windowClosing(WindowEvent we)
                {
                 System.exit(0);
                }
        }